const express = require('express')
const fs = require('fs')
const config = require(process.cwd() + '/src/config/config')
const Game = require(process.cwd() + '/src/class/index')

let router = express.Router()

/* GET users listing. */
router.get('/', function(req, res) {
  // images文件夹里的子文件夹就是所有分类
  let autotiles = []
  fs.promises.readdir(config.autotilesPath).then((files) => {
    const len = files.length
    for (let i = 0; i < len; i++) {
      let autotile = new Game.Autotile(files[i])
      autotiles.push(autotile)
    }
    
    let progresses = autotiles.map(autotile => autotile.getDetail())
    return Promise.all(progresses)
  })
  .then(() => {
    res.json(autotiles)
  })
  .catch((err) => {
    res.sendStatus(500)
    console.log(err)
  })
})

module.exports = router
